package de.sordul.jobMaster.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.inject.Inject;

import de.sordul.jobMaster.IWordlistDAO;
import de.sordul.jobMaster.JobMasterService;

/**
 * A file based implementation of the JobMaster Service
 * @see de.sordul.jobMaster.JobMasterService
 */
public class JobMasterServiceFile implements JobMasterService {

	@Inject
	private IWordlistDAO wordlistDAO;
	private Map<String, Map<String, FragmentVO>> fragments = getFragments();
	private FragmentVO fragment = null;
	
	@Override
	public String getJob(String language, String category) {		 
		if( fragments.get(language) != null && (fragment = fragments.get(language).get(category)) != null ) {
			return getRandomJob(fragment);
		}
		return "";
	}

	/**
	 * Returns all fragments which are located in the fragments resource directory
	 * @return Map (key: language) > Map (key: category) > FragmentVO
	 */
	private Map<String, Map<String, FragmentVO>> getFragments() {
		URL wordlistDir = JobMasterServiceFile.class.getResource("/wordlists");
		File folder;
		String fileName;
		Map<String, Map<String, FragmentVO>> result = new HashMap<String, Map<String, FragmentVO>>();
		
		try {
			folder = new File(wordlistDir.toURI());
			File[] filesList= folder.listFiles();

			// Iterate through all files in the wordlist directory
			for( File f: filesList ) {
				fileName = f.getName();
				if(fileName.substring(2, 3).equals("_")) {
					if(result.get( fileName.substring(0, 2) ) == null) {
						result.put(fileName.substring(0, 2), new HashMap<String, FragmentVO>());
					}
					result.get( fileName.substring(0, 2) ).put(fileName.substring(3, fileName.indexOf(".")), buildFragmentVO(f));
				}
			}
			
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * Build a FragmentVO for given file.
	 * @param file File containing the words
	 */
	private FragmentVO buildFragmentVO(File file) {
		Scanner fileScanner;
		FragmentVO result = new FragmentVO();
		List<String> list = new ArrayList<String>();
		String currentLine;
		int counter = 0;
		
		try {
			fileScanner = new Scanner(file);
			while(fileScanner.hasNextLine()) {
				currentLine = fileScanner.nextLine();
				if(currentLine.isEmpty()) {					
					result.getEntries().put(counter, list);
					list = new ArrayList<String>();
					counter++;
				} else {
					list.add(currentLine);
				}
			}
			result.getEntries().put(counter, list);
			fileScanner.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * Returns a random job generated from the lists within the fragment
	 * @param fragment given FragmentVO
	 * @return Job title
	 */
	private String getRandomJob(FragmentVO fragment) {
		StringBuffer result = new StringBuffer();
		int rndEntryIndex = 0;
		
		for( List<String> entries: fragment.getEntries().values())  {
			rndEntryIndex = (int)(Math.random() * (entries.size()-1));
			result.append(entries.get(rndEntryIndex));
			result.append(" ");
		}
		
		result.setLength(result.length()-1);		
		return result.toString();
	}

}
